/*
 * JerboJournalReport.java
 *
 * Created on April 22, 2008, 12:24 PM
 */

package jerbo;

import java.io.File;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;
import org.jdesktop.application.Action;
import org.jdesktop.application.Application;

/**
 *
 * @author  Luigi Paioro
 */
public class JerboJournalReport extends javax.swing.JDialog {
    
    private java.util.ResourceBundle appBundle = 
            java.util.ResourceBundle.getBundle("jerbo/resources/JerboApp"); // NOI18N
    private SimpleDateFormat dateFormatTxt = new SimpleDateFormat(
            appBundle.getString("Application.date.format"));
    
    
    /** Creates new form JerboJournalReport
     * @param parent 
     */
    public JerboJournalReport(java.awt.Frame parent) {
        super(parent);
        initComponents();
                
        fromDateText.setText(dateFormatTxt.format(new Date()));
        toDateText.setText(dateFormatTxt.format(new Date()));
    }
    
    @Action
    public void close() {
        this.dispose();
    }    
    
    @Action
    public void printDocument() {
        
        String reportSource =  JerboJournalReport.class.getResource(
                "/jerbo/reports/JournalReport.jrxml").toString();
        
        reportSource = reportSource.substring(5);
        
        Map<String, Object> params = new HashMap<String, Object>();
        
        try {
            
            File file = new File(reportSource);

            if (file.exists()) {
            
                JasperReport jasperReport = 
                        JasperCompileManager.compileReport(reportSource);

                java.sql.Connection conn = Application.getInstance(
                        jerbo.JerboApp.class).getConnection();

                SimpleDateFormat dateFormatDb = new SimpleDateFormat("yyyyMMdd");

                Date from = dateFormatTxt.parse(fromDateText.getText());
                Date to = dateFormatTxt.parse(toDateText.getText());


                params.put("FROM_DATE", 
                        Integer.valueOf(dateFormatDb.format(from)));
                params.put("TO_DATE", 
                        Integer.valueOf(dateFormatDb.format(to)));

                // Replace the empty JR datasource parameter with
                // the connection parameter named conn.
                JasperPrint jasperPrint =
                    JasperFillManager.fillReport(jasperReport, params, conn);

                JasperExportManager.exportReportToPdf(jasperPrint);

                JasperViewer.viewReport(jasperPrint, false);
                
                this.dispose();
                
            }
            
        } catch (ParseException ex) {
            Logger.getLogger(JerboJournalReport.class.getName()).log(Level.SEVERE,
                    null, ex);
        } catch (JRException e) {
            e.printStackTrace();
        }

        // Exception handling for the Class.forName method.
        catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }

        // Exception handling for the DriverManager.getConnection method.
        catch (SQLException ex) {
            ex.printStackTrace();
        }     
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fromDateLabel = new javax.swing.JLabel();
        toDateLabel = new javax.swing.JLabel();
        try {
            javax.swing.text.MaskFormatter mf = new javax.swing.text.MaskFormatter(appBundle.getString("Application.date.mask"));
            mf.setPlaceholderCharacter('0');
            fromDateText = new javax.swing.JFormattedTextField(mf);
        } catch (java.text.ParseException pEx) {
            pEx.printStackTrace();
        }
        try {
            javax.swing.text.MaskFormatter mf = new javax.swing.text.MaskFormatter(appBundle.getString("Application.date.mask"));
            mf.setPlaceholderCharacter('0');
            toDateText = new javax.swing.JFormattedTextField(mf);
        } catch (java.text.ParseException pEx) {
            pEx.printStackTrace();
        }
        cancelButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(jerbo.JerboApp.class).getContext().getResourceMap(JerboJournalReport.class);
        setTitle(resourceMap.getString("title")); // NOI18N
        setName("Form"); // NOI18N
        setResizable(false);

        fromDateLabel.setText(resourceMap.getString("fromDateLabel.text")); // NOI18N
        fromDateLabel.setName("fromDateLabel"); // NOI18N

        toDateLabel.setText(resourceMap.getString("toDateLabel.text")); // NOI18N
        toDateLabel.setName("toDateLabel"); // NOI18N

        fromDateText.setName("fromDateText"); // NOI18N

        toDateText.setName("toDateText"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(jerbo.JerboApp.class).getContext().getActionMap(JerboJournalReport.class, this);
        cancelButton.setAction(actionMap.get("close")); // NOI18N
        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N

        okButton.setAction(actionMap.get("printDocument")); // NOI18N
        okButton.setText(resourceMap.getString("okButton.text")); // NOI18N
        okButton.setName("okButton"); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(fromDateText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 93, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(fromDateLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(toDateLabel)
                    .add(toDateText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 93, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(56, Short.MAX_VALUE)
                .add(okButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(cancelButton)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(fromDateLabel)
                    .add(toDateLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(fromDateText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(toDateText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cancelButton)
                    .add(okButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel fromDateLabel;
    private javax.swing.JFormattedTextField fromDateText;
    private javax.swing.JButton okButton;
    private javax.swing.JLabel toDateLabel;
    private javax.swing.JFormattedTextField toDateText;
    // End of variables declaration//GEN-END:variables
    
}
